﻿(function () {
    var app = angular.module('basicFunctions', []);

    $(document).on("click", ".k-overlay", function () {
        var visibleWindow = $(".k-window:visible > .k-window-content");
        if (visibleWindow.length)
            visibleWindow.data("kendoWindow").close();
    });

    addExtraStylingToGrid = function () {
        $(".k-grid-content > table > tbody > tr").hover(
            function () {
                $(this).toggleClass("k-state-hover");
            }
        );
    };

    window.addEventListener("keydown", function (e) {
        // space and arrow keys
        if ([37, 38, 39, 40].indexOf(e.keyCode) > -1) {
            //TODO
            e.preventDefault();
        }
    }, false);

    app.controller('AutocompleteController', function ($scope, Services, $http) {
        $scope.serviceUri = "http://efm.develop.local/api/";

        $scope.init = function (name, type, id, Vessel) {
            $scope.info = { name: name, type: type, id: id, Vessel: Vessel };
            $scope.length = 3;
            $scope.styleUnloader = { display: 'none' };
            $scope.styleLoader = { display: 'inline-block' };
            $scope.currStyle = $scope.styleUnloader;
        };

        $scope.getData = function () {
            if ($scope.model.length == $scope.length) {
                $scope.currStyle = $scope.styleLoader;
                $http({
                    url: $scope.serviceUri + 'fixtureextend/' + $scope.model,
                    method: "GET"
                }).success(function (data) {
                    var autocomplete = $("#" + $scope.info.type + $scope.info.name + $scope.info.id).data("kendoAutoComplete");
                    autocomplete.setDataSource(new kendo.data.DataSource({
                        data: data
                    }));
                    autocomplete.search($scope.model);
                    $scope.currStyle = $scope.styleUnloader;
                }).error(function (data) {
                    $scope.currStyle = $scope.styleUnloader;
                });
            }
            else if ($scope.model.length < $scope.length) {
                var autocomplete = $("#" + $scope.info.type + $scope.info.name + $scope.info.id).data("kendoAutoComplete");
                autocomplete.refresh();
            }
        };

        $scope.autocomplete = {
            dataTextField: "Name",
            minLength: 3,
            delay: 1000,
            select: function (e) {
                Services.setOC({
                    data: this.dataItem(e.item.index()), name: $scope.info.name,
                    type: $scope.info.type, id: $scope.info.id
                });
            },
            //dataSource: {
            //    type: "json",
            //    serverFiltering: true,
            //    transport: {
            //        read: {
            //            url: function (options) {
            //                return $scope.serviceUri + 'fixtureextend/' + $scope.model;
            //            },
            //            cache: true,
            //            dataType: "json"
            //        }
            //    }
            //}
        };

        $scope.getVesselData = function () {
            if ($scope.info.Vessel.length == $scope.length) {
                $scope.currStyle = $scope.styleLoader;
                $http({
                    url: $scope.serviceUri + 'vessel/' + $scope.info.Vessel,
                    method: "GET"
                }).success(function (data) {
                    var autocomplete = $("#vessel" + $scope.info.id).data("kendoAutoComplete");
                    autocomplete.setDataSource(new kendo.data.DataSource({
                        data: data
                    }));
                    autocomplete.search($scope.info.Vessel);
                    $scope.currStyle = $scope.styleUnloader;
                }).error(function (data) {
                    $scope.currStyle = $scope.styleUnloader;
                });
            }
            else if ($scope.info.Vessel.length < $scope.length) {
                var autocomplete = $("#vessel" + $scope.info.id).data("kendoAutoComplete");
                autocomplete.refresh();
            }
        };

        $scope.autocompleteVessel = {
            dataTextField: "Name",
            minLength: 3,
            delay: 1000,
            select: function (e) {
                Services.changeOrderDate({
                    date: this.dataItem(e.item.index()), name: 'Vessel',
                    type: $scope.info.type, Id: $scope.info.id
                });
            }
            //dataSource: {
            //    type: "json",
            //    serverFiltering: true,
            //    transport: {
            //        read: {
            //            url: function (options) {
            //                return $scope.serviceUri + 'Vessel';
            //            },
            //            cache: true,
            //            dataType: "json"
            //        },
            //        parameterMap: function (data, type) {
            //            if (type == "read") {
            //                return { name: $scope.info.Vessel };
            //            }
            //        }
            //    }
            //}
        };
    });

    app.controller('AccountController', function ($scope, Services) {

        $scope.init = function (data, oper, type, id) {
            $scope.acc = { data: data, oper: oper, type: type, id: id, fixtureName: '', selectedValue: null };
        };

        $scope.$on('setOC', function () {
            if ($scope.acc.type == Services.OC.type && $scope.acc.id == Services.OC.id) {
                if (Services.OC.name == 'owner') {
                    $scope.acc.data = Services.OC.data;
                }
                else {
                    $scope.acc.oper = Services.OC.data;
                }
                $scope.setParent();
            }
        });

        $scope.changeSelection = function (keyEvent) {
            if (keyEvent.which == 9 && $scope.acc.type != 'owner') {
                var obj = $('#ownerowner' + $scope.acc.id);
                obj.focus();
                obj.select();
                keyEvent.preventDefault();
            }
        };

        $scope.changedContact = function (value) {
            if (value != undefined) {
                $scope.acc.oper.SelectedContactId = value.Id;
                Services.boxCheck({ id: $scope.acc.id });
            }
        };

        $scope.setParent = function () {
            var name = '';
            if(Services.OC.type=='owner'){
                if(Services.OC.name=='owner')
                    name = 'OW';
                else
                    name = 'OWOP';
            }
            else
                if(Services.OC.name=='owner')
                    name = 'CH';
                else
                    name = 'CHOP';
            Services.changeOrderDate({ Id: $scope.acc.id, date: Services.OC.data, name: name });
        };

    });

    app.controller('functionsCtrl', function ($scope) {

        $scope.monthSelectorOptions = {
            start: "year",
            depth: "year",
        };
        $scope.getType = function (x) {
            return typeof x;
        };
        $scope.isDate = function (x) {
            return x instanceof Date;
        };

        $scope.clearDate = function () {
            return null;
        };

        $scope.getDate = function (date) {
            return new Date(date);
        };
    });
})()